package com.augurit.gzcgwsp.csshlj.web;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.commons.fileupload.DiskFileUpload;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.orm.Page;
import org.springside.modules.web.struts2.Struts2Utils;

import com.augurit.common.base.action.CrudActionSupport;
import com.augurit.common.util.lang.DateUtils;
import com.augurit.common.util.lang.StringUtils;
import com.augurit.gzcgwsp.csshlj.entity.Person;
import com.augurit.gzcgwsp.csshlj.service.IPersonService;
import com.augurit.wf.common.constant.WfConstant;
import com.augurit.wf.common.web.WfBusClientExtAction;

@SuppressWarnings( { "serial", "unchecked" })
@Results( {
		@Result(name = CrudActionSupport.RELOAD, location = "csshlj-cz-person.action", type = "redirect"),
		@Result(name = WfConstant.PRINT, location = WfConstant.PRINT_JSP),
		@Result(name = WfConstant.PRINT_DIR, location = WfConstant.PRINT_DIR_JSP),
		@Result(name ="viewperson", location = "/gzcgwsp/csshlj/viewperson.jsp"),
		@Result(name = WfConstant.MAKE_DOCUMENT, location = "/gzcgwsp/wfex/client/common/make-rqjyxk-document.jsp") })
/**
 * 城市生活垃圾处置专业人员
 */
public class CsshljCzPersonAction extends WfBusClientExtAction {
	 private static final int FILE_SIZE=16*1024;
	@Autowired
	private IPersonService personService;
	private Page<Person> page;
	private File xlfile;
	private File jlfile;
	private String jlfileFileName;//简历附件名称
	private String xlfileFileName;//学历附件名称
	public File getJlfile() {
		return jlfile;
	}
	public void setJlfile(File jlfile) {
		this.jlfile = jlfile;
	}



	/**
	 * 分页统计简历人员
	 * @return
	 */
    public String toListPerson(){
    	//请求的查询数据
    String id =getRequest().getParameter("id");
    if(page==null){
    	page = new Page<Person>(10);
  	 }
    if(StringUtils.isNotBlank(id)){
    	page=this.personService.toListPerson(page,Long.valueOf(id));
    	  renderText(Struts2Utils.getJsonString(page, DateUtils.YYYY_MM_DD_HH_MM_SS));
    	  	
    }
  	return null;
  }
    
    /**
     * 删除专业人员
     * @return
     * @throws IOException 
     */
    public String  delPerson() throws IOException{
    	String personId=getRequest().getParameter("personId");
    	if(StringUtils.isNotBlank(personId)){
    		boolean flag=personService.deleteById(Long.valueOf(personId));
    		getResponse().getWriter().print(flag);
    	}
    	return null;
    }
    
    /**
     * 获取专业人员
     * @return
     * @throws IOException
     */
    public String getPerson()  throws IOException{
    	String personId=getRequest().getParameter("personId");
    	if(StringUtils.isNotBlank(personId)){
    		Person person=personService.getEntity(Long.valueOf(personId));
    		JSONObject jsonObject = JSONObject.fromObject(person);
    		HttpServletResponse response=ServletActionContext.getResponse();
    		response.setCharacterEncoding("utf-8");
    		String jsonString=jsonObject.toString();
    		response.getWriter().print(jsonString);		
    	}
		
    	return null;
    }
    
    /**
     * 获取专业人员
     * @return
     * @throws IOException
     */
    public String viewPerson()  throws IOException{
    	String personId=getRequest().getParameter("personId");
    	if(StringUtils.isNotBlank(personId)){
    		Person person=personService.getEntity(Long.valueOf(personId));
    		getRequest().setAttribute("person", person);
    	}
    	return "viewperson";
    }
    
    
    
    

    /**
     * 新增专业技术人员
     * @return
     * @throws IOException 
     */
	public String saveOrUpdatePerson() throws IOException{
		  String filePath=ServletActionContext.getServletContext().getRealPath("testFile")
		  +"/"+this.jlfileFileName;
		  
		  File targetFile=new File(filePath);
		  upLoadFile(this.jlfile,targetFile);
		Map map=this.getMapByRequest(getRequest());
		String contextpath=getRequest().getSession().getServletContext().getRealPath("/");
		map.put("xlFileName", xlfileFileName);
		map.put("jlFileName", jlfileFileName);
		map.put("contextPath", contextpath);
		boolean flag=personService.saveOrUpdatePerson(map,jlfile,xlfile);
		if(flag)
			getResponse().getWriter().print("{success:true,msg:'true'}");
		else
			getResponse().getWriter().print("{success:true,msg:'false'}");
		return null;
	}
	
	
	 public void upLoadFile(File source,File target){
		  InputStream in=null;
		  OutputStream out=null;
		  try{
		   in=new BufferedInputStream(new FileInputStream(source),FILE_SIZE);
		   out=new BufferedOutputStream(new FileOutputStream(target),FILE_SIZE);
		   byte[] image=new byte[FILE_SIZE];
		   while(in.read(image)>0){
		    out.write(image);
		   }
		  }catch(IOException ex){
		   ex.printStackTrace();
		  }finally{
		   try{
		    in.close();
		    out.close();
		   }catch(IOException ex){
		    
		   }
		  }
		 }

	private  Map getMapByRequest(HttpServletRequest request) {
	        Map<String, String> map = new HashMap<String, String>();
	        Enumeration enu = request.getParameterNames();
	        while(enu.hasMoreElements()) {   
	            String paraName = (String)enu.nextElement();   
	            String paraValue = request.getParameter(paraName).trim(); 
	            if(paraValue!=null && !"".equals(paraValue)){
	                map.put(paraName, paraValue);
	            }           
	        }
	                
	        return map;
	    }

	
	
	
	@Override
	public String delete() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String list() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	protected void prepareModel() throws Exception {
			
	}
	
	

	@Override
	public String save() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	public Object getModel() {
		
		return null;
	}
	public File getXlfile() {
		return xlfile;
	}
	public void setXlfile(File xlfile) {
		this.xlfile = xlfile;
	}

	public String getJlfileFileName() {
		return jlfileFileName;
	}
	public void setJlfileFileName(String jlfileFileName) {
		this.jlfileFileName = jlfileFileName;
	}
	public String getXlfileFileName() {
		return xlfileFileName;
	}
	public void setXlfileFileName(String xlfileFileName) {
		this.xlfileFileName = xlfileFileName;
	}
	@Override
	public String wfBusSave() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}
}
